//
// Created by User on 24-10-3.
//

#ifndef GENERAL_DEMUXER_H
#define GENERAL_DEMUXER_H

#include "demuxer_interface.hpp"
#include "packet_provider.hpp"
#include "demuxer_error.hpp"
#include "media_packet.h"
#include <string>
#include <vector>
#include <memory>
#include <queue>

extern "C" {
struct AVFormatContext;
struct AVPacket;
}

namespace demuxer
{
    class general_demuxer
        : public demuxer_interface
    {
    public:
        explicit general_demuxer(const std::string& path);

        ~general_demuxer() override;

        utils::media_packet get_packet(int stream_index) override;

        auto get_streams() -> std::vector<std::unique_ptr<interfaces::packet_provider>>&;

    private:
        void init_demuxer(const std::string& path);

        void clean_demuxer(error::demuxer_error::type _type);

        void read_frame(std::queue<utils::media_packet>& que);

    private:
        AVFormatContext* m_fmt_ctx{};
        AVPacket* m_pkt{};
        std::vector<std::unique_ptr<interfaces::packet_provider>> m_streams;
        std::vector<std::queue<utils::media_packet>> m_queues;
        bool is_end{};
    };
} // demuxer

#endif //GENERAL_DEMUXER_H
